package com.qq.service;

import com.qq.dao.UserDao;
import com.qq.pojo.*;

import javax.swing.*;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Hashtable;

/**
 * @author： Aeolia
 * @Description： TODO
 * @Date： 2021/10/17 1:19
 */
public class ServerService extends Thread {

    private static Hashtable<String, Socket> clientSockets;
    private Socket socket;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ServerService(Socket socket) {
        this.socket = socket;
        try {
            ois = new ObjectInputStream(this.socket.getInputStream());
            oos = new ObjectOutputStream(this.socket.getOutputStream());
            clientSockets=new Hashtable<>();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("获取Socket对象IO流失败");
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Message message = (Message) this.ois.readObject();
                // 根据不同的消息类型做具体处理
                switch (message.getMessageType()){
                    case CHAT ->   chat(message);
                    case LOGIN -> login(message);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    // 响应
    private void response(Message message) {

    }
    // 登录请求
    private void login(Message message) {
        User loginUser = (User) message.getData();
        // 查询数据库并验证
        UserDao userDao = new UserDao();
        User user = userDao.findUserByAccountAndPassword(loginUser.account, loginUser.password);
        if (null!=user){// 验证用户
            System.out.println("用户验证成功");
            // 返回成功响应结果
            sendMessage(new Message(CODE.LOGIN_SUCCESS,MessageType.RESPONSE,user),oos);//响应结果
            putInClientSockets(user.account,socket);
        }else{
            System.out.println("用户验证失败");
            // 返回失败响应结果
            sendMessage(new Message(CODE.LOGIN_FAIL,MessageType.RESPONSE,null),oos);
        }

    }

    // 聊天消息
    private void chat(Message message){
        // 解析出信息找到接收者的Socket再发送给接收者
        try {
            ChatMessage messageData = (ChatMessage) message.getData();
            String receiveUserAccount = messageData.getReceiveUserAccount();
            Socket socket = ServerService.clientSockets.get(receiveUserAccount);
            sendMessage(message,socket);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务器消息解析失败");
        }

    }

    private void sendMessage(Message message,Socket socket){
        try {
            ObjectOutputStream oosToClient = new ObjectOutputStream(socket.getOutputStream());
            oosToClient.writeObject(message);
            System.out.println("服务器发送消息成功");
        } catch (IOException e) {
            System.out.println("服务器消息发送失败");
            e.printStackTrace();
        }
    }

    private void sendMessage(Message message,ObjectOutputStream oos){
        try {
            oos.writeObject(message);
            System.out.println("服务器发送消息成功");
        } catch (IOException e) {
            System.out.println("服务器消息发送失败");
            e.printStackTrace();
        }
    }

    public void putInClientSockets(String userAccount,Socket socket){
        ServerService.clientSockets.put(userAccount,socket);
    }



}
